import re
from datetime import datetime

from crawler.core import BaseSpider
from scrapy import Request, FormRequest
from crawler.utils.func_tools import md5, get_public_item

# 初步完成
__all__ = ['YooxMainSpider']

from spiders.electronic_business.yoox.extractors.extractor import YooxMainExtractor, YooxDetailExtractor


class YooxMainSpider(BaseSpider):
    name = "electronic_business_main_YOOX"

    redis_key = 'electronic_business_main_YOOX'
    default_origin_request_type = 'FormRequest'
    default_proxy_type = 'tunnel'
    default_proxy_demote = False
    default_origin_header = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'}
    custom_settings = {
        'COOKIES_ENABLED': False,
        'CONCURRENT_REQUESTS': 5,
        'ITEM_PIPELINES': {
            'crawler.pipelines.ElectronicBusinessPipeline': 100,
        }
    }

    def _get_request_formdata(self, task):
        url = task['category_info']['href']
        sex = task['category_info']['sex']
        result = re.compile('dept=(.*)').findall(url)
        types = ''
        if result:

            if '&' in result[0]:
                types = result[0].split('&')[0]
            else:
                types = result[0]

        if 'men' in url or 'boy' in url:
            gender = 'U'
        else:
            gender = 'D'
        data = {
            'dept': types,
            'gender': gender,
            'page': '1',
            'season': 'X',
            'clientabt': 'SmsMultiChannel_ON,myooxNew_ON,cartPageNew_OFF'
        }
        task['data'] = data
        task['sex'] = sex
        return data

    def _get_request_url(self, task):
        url = task['category_info']['href']
        return url

    def parse(self, response, **kwargs):
        self.logger.info(f'Request End, URL: {response.url}')
        task = response.meta['task']
        total_page = response.xpath('//a/@data-total-page').extract()
        if total_page:
            for page in range(1, int(total_page[0]) + 1):
                data = task['data']
                data['page'] = str(page)
                yield FormRequest(
                    url=task['category_info']['href'],
                    headers=self.default_origin_header,
                    formdata=data,
                    method='GET',
                    meta={
                        'task': task,
                        'proxy_type': self.default_proxy_type
                    },
                    callback=self.parse_html,
                    dont_filter=True
                )
        else:
            result = YooxMainExtractor.get_item(response)
            for info_dict in result:
                url = info_dict['url']
                if not url:
                    continue
                yield Request(
                    url=url,
                    headers=self.default_origin_header,
                    method='GET',
                    meta={
                        'task': task,
                        'info_dict': info_dict,
                        'proxy_type': self.default_proxy_type
                    },
                    callback=self.parse_detail,
                    dont_filter=True
                )

    def parse_html(self, response):
        self.logger.info(f'Request End, URL: {response.url}')
        task = response.meta['task']
        result = YooxMainExtractor.get_item(response)
        for info_dict in result:
            url = info_dict['url']
            if not url:
                continue
            yield Request(
                url=url.split('#')[0],
                headers=self.default_origin_header,
                method='GET',
                meta={
                    'task': task,
                    'info_dict': info_dict,
                    'proxy_type': self.default_proxy_type
                },
                callback=self.parse_detail,
                dont_filter=True
            )

    def parse_detail(self, response):
        self.logger.info(f'Request End, URL: {response.url}')
        task = response.meta['task']
        info_dict = response.meta['info_dict']
        spu_id = info_dict['spu_id']
        num = spu_id[0:2]
        res = re.compile('type="application/json">(.*?)</script>', re.S).findall(response.text)[0]
        composition, end_list, product_des, in_dict = YooxDetailExtractor.get_item(response, res, num)
        info_dict['color'] = in_dict['color']
        info_dict['atlas'] = in_dict['atlas']
        info_dict['composition'] = composition
        info_dict['product_des'] = product_des
        info_dict['size_stock'] = end_list
        info_dict['category'] = task['category_info']['category']
        info_dict['origin'] = 'yoox_HKG'
        info_dict['md5_value'] = md5(info_dict['spu_id'] + info_dict['origin'])
        info_dict['sync'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

        item = get_public_item(task, table_name='shedana_data')
        item['item'] = info_dict
        yield item
